package com.purchase.manage.controller;

import com.purchase.common.annotation.Log;
import com.purchase.common.core.controller.BaseController;
import com.purchase.common.core.domain.AjaxResult;
import com.purchase.common.core.page.TableDataInfo;
import com.purchase.common.core.text.Convert;
import com.purchase.common.enums.BusinessType;
import com.purchase.manage.damian.DAO.SupplierDAO;
import com.purchase.manage.damian.Purchase;
import com.purchase.manage.damian.Supplier;
import com.purchase.manage.service.IManageService;
import com.purchase.system.domain.SysOperLog;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

import static com.purchase.common.utils.PageUtils.startPage;

/**
 * @author oyi46
 * @version 1.0
 * Create by 2023/7/4 13:33
 */

@RestController
@RequestMapping("/manage")
public class ManageController extends BaseController {

    @Autowired
    IManageService manageService;

    @PreAuthorize("@ss.hasPermi('manage:manage:manage')")
    @Log(title = "供应商列表查询（审核页面）", businessType = BusinessType.SELECT)
    @GetMapping("/examineList")
    public TableDataInfo examineSupplierList(SupplierDAO supplierDAO){
        startPage();
        List<Supplier> list = manageService.selectSupplierList(supplierDAO);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('manage:manage:manage')")
    @Log(title = "供应商审核", businessType = BusinessType.UPDATE)
    @GetMapping("/examine")
    public AjaxResult examineSupplier(SupplierDAO supplierDAO){
        if (manageService.examineSupplier(supplierDAO) ==0){
            return error("供应商审核失败，请重新审核");
        }
        return success();
    }

    @PreAuthorize("@ss.hasPermi('manage:manage:manage')")
    @Log(title = "供应商多个删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/suppliers")
    public AjaxResult deleteSupplierIds(String ids){
        String[] tableNames = Convert.toStrArray(ids);
        manageService.deleteSupplierIds(tableNames);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('manage:manage:manage')")
    @Log(title = "采购员多个删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/purchases")
    public AjaxResult deletePurchaseIds(String ids){
        String[] tableNames = Convert.toStrArray(ids);
        manageService.deletePurchaseIds(tableNames);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('manage:manage:manage')")
    @Log(title = "查看员工列表", businessType = BusinessType.SELECT)
    @GetMapping("/purchaseList")
    public TableDataInfo selectPurchaseList(Purchase purchase){
        startPage();
        List<Purchase> list = manageService.selectPurchaseList(purchase);
        return getDataTable(list);
    }

}
